<?php
class game_quan extends base{

	public function init(){
		parent::init();

		$this->quanObj = D('game_quan');
		$this->quan_logObj = D('game_quan_log');
		$this->quan_cateObj = D('game_quan_cate');
		$this->game_listObj = D('game_list');
		$this->userObj = D('sys_user');
		$this->upload_fileObj = D('upload_file');

		$this->cates = $this->quan_cateObj->findAll(array("status=1"));
	}

	public function index() 
	{
		
		$data = array();
		$game_id = arg('game_id');
		$cate_id = arg('cate_id');
		$info_id = arg('info_id');
		$keyword = arg('keyword');
		
		$cond = "status=1 ";
		
		if($game_id){
			$cond .=" and game_id={$game_id}";
		}
		if($cate_id){
			$cond .=" and cate_id={$cate_id}";
		}
		if($info_id){
			$cond .=" and id={$info_id}";
		}
		//搜索
		if($keyword){
			$cond.=" and (title like '%".$keyword."%' or intro like '%".$keyword."%') ";
		}
		
		$result = $this->quanObj->get_datalist($cond,"is_top desc, up_time desc");
		// dump($result);exit;
		if($result['list']){
			$data['list'] = $result['list'];
			$this->renderSuccess('获取成功','',$data);
		}else{
			$this->renderError('数据错误');
		}
		
	}

	public function siteinfo() 
	{

		//分类
		foreach($this->cates as $val){
			$v['value'] = intval($val['id']);
			$v['text'] = $val['name'];
			$cates[] = $v;
		}
		$data['cates'] = $cates;
		// dump($this->game);exit;
		$site = [
			'name' => $this->game['name'],
			'title' => '交流广场',
			'gonggao' => '请勿发布虚假信息、感谢您的配合。',
			'logo' => $this->game['logo'],
			'adimg' => $this->game['photos'][0],
			'sharetitle' => $this->game['name'],
		];
		$data['site'] = $site;
		// $data['ads'] = $ads;
		// $data['city'] = $this->city;
		$this->renderSuccess('获取成功','',$data);
	}


	public function catelist() 
	{
		$cond = "status=1";
		$cates = $this->quan_cateObj->findAll(array($cond),'sort desc,id asc');
		$data['cates'] = $cates;

		//分类
		foreach($cates as $val){
			$v['text'] = $val['name'];
			$v['value'] = intval($val['id']);
			$catelist[] = $v;
		}
		$data['catelist'] = $catelist;

		$this->renderSuccess('获取成功','',$data);
	}

	public function quanlist() 
	{
		
		$data = array();
		$cate_id = arg('cate_id');
		$game_id = arg('game_id');
		$keyword = arg('keyword');
		
		$cond = "status=1 ";
		if($cate_id){
			$cond .=" and cate_id={$cate_id}";
		}
		if($game_id){
			$cond .=" and game_id={$game_id}";
		}
		//搜索
		if($keyword){
			$cond.=" and (title like '%".$keyword."%' or intro like '%".$keyword."%') ";
		}
		
		$result = $this->quanObj->get_datalist($cond,"is_top desc, up_time desc");
		if($result['list']){
			$data['list'] = $result['list'];
			$this->renderSuccess('获取成功','',$data);
		}else{
			$this->renderError('数据错误');
		}
		
		
	}

	public function mylist() 
	{
		

		$data = array();
		$cate_id = arg('cate_id');
		$game_id = arg('game_id');
		$keyword = arg('keyword');
		
		$cond = "status=1 ";
		if($cate_id){
			$cond .=" and cate_id={$cate_id}";
		}
		if($game_id){
			$cond .=" and game_id={$game_id}";
		}
		//搜索
		if($keyword){
			$cond.=" and (title like '%".$keyword."%' or intro like '%".$keyword."%') ";
		}

		// 我的关注
		if($this->user){
			$cond = "user_id='{$this->user['id']}'";
			$result = $this->game_subObj->get_list($cond);
			if($result){
				foreach($result as $val){
					$game_ids[] = $val['game_id'];
				}
				$game_ids = implode(',',$game_ids);
				$cond .= " and game_id IN ({$game_ids})";
			}else{
				$this->renderError('没有数据');
			}
		}
		// dump($cond);exit;
		$result = $this->quanObj->get_datalist($cond,"is_top desc, up_time desc");
		if($result['list']){
			$data['list'] = $result['list'];
			$this->renderSuccess('获取成功','',$data);
		}else{
			$this->renderError('数据错误');
		}
		
		
	}

	public function quaninfo()
	{
		$info_id = arg('info_id');
		
		$cond = "id='{$info_id}' and status=1";
		$result = $this->quanObj->get_info($cond);
		if($result){
			$val = $result;
			if($val['imgs']){
				$photos_arr = explode(',', $val['imgs']);
				foreach($photos_arr as $v){
					// $val['imgss'][] = $v;
					$img_url = D('upload_file')->get_image_url($v);
					$val['imgss'][] = $img_url;
					$val['thumbs'][] = $img_url."_thumb.jpg";
				}
				$val['img'] = $val['imgss'][0];
			}
			$val['content'] = __htmlToArray($val['content']);

			$val['cate'] = $this->quan_cateObj->get_info($val['cate_id']);
			$val['game'] = $this->gameObj->get_info($val['game_id']);
			$val['game']['logo'] = D('upload_file')->get_image_url($val['game']['logo']);
			$val['user'] = $this->userObj->get_info($val['user_id']);
			$data['info'] = $val;
			
			$this->quanObj->incr(array('id'=>$info_id),'hits');

			$this->renderSuccess('获取成功','',$data);
		}else{
			$this->renderError('数据错误');
		}
	}

	public function add()
	{
		if($this->fmdo=='add' && $this->user){

			
			$user_id = $this->user['id'];
			$formData = $this->formData('formData');

			$checkText = $this->userObj->checkText($this->setting['wxapp'],arg('formData'));
			if($checkText['errcode'] == 87014){
				$this->renderError('提交内容不安全');
			}

			$photo = joinStr($formData['photos']);
			//标签
			// $tag = $formData['tag'];
			// if($tag){
			// 	$tags = $this->house_tagObj->add_tag($this->game_id,$tag);
			// }
			
			
			$arrBase = array(
				'game_id'=>$this->game['id'],
				'cate_id'=>$formData['cate_id'],
				'user_id'=>$user_id,
				'title'=>$formData['title'],
				'intro'=>$formData['intro'],
				'photo'=>$photo,
				'add_time' =>date('Y-m-d H:i:s'),
				'up_time' =>date('Y-m-d H:i:s'),
				'status' =>1,
				// 'house_score'=>$score,
			);
			$info_id = $this->quanObj->add($arrBase);
			$data['info_id'] = $info_id;
			$this->renderSuccess('发布成功','',$data);
		}
	}

	public function dianzan()
	{
		if($this->fmdo  && $this->user){
			$order_status = 1;

			$info_id = arg('info_id');
			$user_id = $this->user['id'];

			if($info_id && $user_id){
				$log_id = $this->quan_logObj->dianzan($info_id,$user_id);

				$data['log_id'] = $log_id;
				$this->renderSuccess('点赞成功','',$data);
			}
		}
	}

	public function pinglun(){
		if($this->fmdo && $this->user){

			$user_id = $this->user['id'];
			$formData = $this->formData('formData');


			$checkText = $this->userObj->checkText($this->setting['wxapp'],arg('formData'));
			if($checkText['errcode'] == 87014){
				$this->renderError('提交内容不安全');
			}

			$order_status = 1;
			$intro = $formData['content'];
			$to_user_id = $formData['to_user_id'] ?? 0 ;
			$info_id = $formData['info_id'];
			if($to_user_id == $user_id){
				$to_user_id = 0;
			}

			if($info_id && $user_id && $intro){
				$log_id = $this->quan_logObj->pinglun($info_id,$user_id,$to_user_id,$intro);
				
				$data['log_id'] = $log_id;
				$this->renderSuccess('评论成功','',$data);
			}
		}
	}


	

	

	//拨打电话
	public function calltel()
	{
		
		if($this->fmdo=='calltel'){

			$user_id = $this->user['id'];
			$info_id = arg('info_id');
			// $log_id = $this->house_logObj->calltel_log($info_id,$user_id);
			// if($log_id){
			// 	$this->renderSuccess('操作成功','',$log_id);
			// }
			$log_id = 0;
			$this->renderSuccess('操作成功','',$log_id);
		}
	}

	public function yuyue()
	{

		if($this->fmdo == 'add'){

			$formData = $this->formData('formData');
			$yy_day = strtotime($formData['yy_day']);
			$yy_day = date('Y-m-d H:i:s',$yy_day);
			
			$info_id = arg('info_id');
			$cond = "id='{$info_id}' and status=1";
			$result = $this->xinfangObj->get_info($cond);
			if($result){
				$arrBase = array(
					'city_id' => $result['city_id'],
					'area_id' => $result['area_id'],
					'info_id' => $result['id'],
					'user_name' => $formData['user_name'],
					'user_tel' => $formData['user_tel'],
					'yy_day' => $yy_day,
					'add_time' => date('Y-m-d H:i:s'),
					'user_id' => $this->user['id'],
					'status' => 1,
				);
				$house_id = $this->xinfang_yuyueObj->add($arrBase);
				$this->renderSuccess('预约成功');
			}else{
				$this->renderError('数据错误');
			}
			
		}
		
	}

}